package com.rong.ruiji.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.rong.ruiji.common.R;
import com.rong.ruiji.entity.User;
import com.rong.ruiji.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService ;

    @PostMapping("/login")
    public R<String> login(HttpServletRequest request, @RequestBody User user) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper( );
        queryWrapper.eq(User::getPhone, user.getPhone());
        User getUser = userService.getOne(queryWrapper);
        if(getUser == null) {
            userService.save(user);
            getUser = userService.getOne(queryWrapper);
        }
        request.getSession().setAttribute("user",getUser.getId());
        return  R.success("成功");
    }
}
